﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LootPoints.Security;
using LootPoints.Warcraft;

namespace LootPoints.Pages
{
    public partial class Overview : Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try { if (!(bool)Session["isadmin"]) { lbtnAdmin.Visible = false; lbtnUpdate.Visible = false; } }
            catch { lbtnAdmin.Visible = false; lbtnUpdate.Visible = false; }

            try { if (!(bool)Session["logged"]) { lbtnLogoff.Visible = false; lbtnLogin.Visible = true; }
                  else { lbtnLogoff.Visible = true; lbtnLogin.Visible = false; } }
            catch { lbtnLogoff.Visible = false; lbtnLogin.Visible = true; }

            Reload();
        }

        protected void Reload()
        {
            while (table.Rows.Count != 2) table.Rows.RemoveAt(table.Rows.Count - 1);

            ModelContainer context = new ModelContainer();
            List<Character> characters = (from c in context.Character orderby c.Name ascending select c).ToList();
            RSReference reference = (from c in context.RSReference select c).FirstOrDefault();
            if (reference == null)
            {
                reference = new RSReference();
                reference.Name = "default";
                context.RSReference.Add(reference);
                context.SaveChanges();
            }
            foreach (Character character in characters)
            {
                TableRow row = new TableRow() { CssClass = "Row" };
                row.Cells.Add(new TableCell() { Text = character.Group, HorizontalAlign = HorizontalAlign.Left });
                row.Cells.Add(new TableCell());
                row.Cells.Add(new TableCell() { Text = "<a href='Characterpage.aspx?char=" + character.Name + "'>" + character.Name + "</a>", HorizontalAlign = HorizontalAlign.Left });
                row.Cells.Add(new TableCell() { Text = character.EP.ToString(), HorizontalAlign = HorizontalAlign.Center });
                row.Cells.Add(new TableCell() { Text = character.GP.ToString(), HorizontalAlign = HorizontalAlign.Center });
                string rp; try { rp = Convert.ToDecimal((Convert.ToDecimal(character.EP) / Convert.ToDecimal(character.GP))).ToString("#.###"); }
                catch { rp = "0"; }
                row.Cells.Add(new TableCell() { Text = rp, HorizontalAlign = HorizontalAlign.Center });

                string score = "";
                try { score = (from c in context.RaidScore where c.Character.Name == character.Name select c).FirstOrDefault().Total.ToString(); }
                catch { score = "none"; }

                row.Cells.Add(new TableCell());
                try
                {
                    if ((bool)Session["isadmin"])
                        row.Cells.Add(new TableCell() { Text = "<a href='RaidscoreSheet.aspx?char=" + character.Name + "'>" + score + "</a>", HorizontalAlign = HorizontalAlign.Center });
                    else
                        row.Cells.Add(new TableCell() { Text = score, HorizontalAlign = HorizontalAlign.Center });
                }
                catch
                {
                    row.Cells.Add(new TableCell() { Text = score, HorizontalAlign = HorizontalAlign.Center });
                }
                table.Rows.Add(row);
            }

            SortList();
        }
        protected void SortList()
        {
            List<TableRow> unsorted = new List<TableRow>();
            for (int i = 2; i < table.Rows.Count; i++) unsorted.Add(table.Rows[i]);

            while (table.Rows.Count != 2) table.Rows.RemoveAt(table.Rows.Count - 1);

            foreach (TableRow row in unsorted)
                if (row.Cells[5].Text == "")
                    row.Cells[5].Text = "0";

            List<TableRow> sorted = unsorted.OrderBy(c => Convert.ToDecimal(c.Cells[5].Text)).ToList();
            for (int i = sorted.Count - 1; i >= 0; i--) table.Rows.Add(sorted[i]);
        }

        protected void lbtnAdmin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Adminpage.aspx");
        }
        protected void lbtnLogoff_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("~/Default.aspx");
        }
        protected void lbtnUpdate_Click(object sender, EventArgs e)
        {
            tbLog.Text = "";
            mpeLog.Show();
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (tbLog.Text != "")
            {
                if (EpGpLog.SaveLog(tbLog.Text)) { mpeLog.Hide(); tbLog.Text = ""; Reload(); }
                else mpeLog.Show();
            }
            else mpeLog.Show();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            tbLog.Text = "";
            mpeLog.Hide();
        }
        protected void lbtnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Login.aspx");
        }

        protected void tbFilterGR_TextChanged(object sender, EventArgs e)
        {
            for (int i = 2; i < table.Rows.Count; i++)
                table.Rows[i].Visible = true;

            if (tbFilterGR.Text == "") tbFilterGR.Text = "filter...";
            else
            {
                for (int i = 2; i < table.Rows.Count; i++)
                    if (!table.Rows[i].Cells[0].Text.ToLower().Contains(tbFilterGR.Text.ToLower())) table.Rows[i].Visible = false;
            }
        }
        protected void tbFilterCH_TextChanged(object sender, EventArgs e)
        {
            for (int i = 2; i < table.Rows.Count; i++)
                table.Rows[i].Visible = true;

            if (tbFilterCH.Text == "") tbFilterCH.Text = "filter...";
            else
            {
                for (int i = 2; i < table.Rows.Count; i++)
                    if (!table.Rows[i].Cells[2].Text.ToLower().Contains(tbFilterCH.Text.ToLower())) table.Rows[i].Visible = false;
            }
        }
        protected void tbFilterEP_TextChanged(object sender, EventArgs e)
        {
            for (int i = 2; i < table.Rows.Count; i++)
                table.Rows[i].Visible = true;

            if (tbFilterEP.Text == "") tbFilterEP.Text = "filter...";
            else
            {
                for (int i = 2; i < table.Rows.Count; i++)
                    if (!table.Rows[i].Cells[3].Text.Contains(tbFilterEP.Text)) table.Rows[i].Visible = false;
            }
        }
        protected void tbFilterGP_TextChanged(object sender, EventArgs e)
        {
            for (int i = 2; i < table.Rows.Count; i++)
                table.Rows[i].Visible = true;

            if (tbFilterGP.Text == "") tbFilterGP.Text = "filter...";
            else
            {
                for (int i = 2; i < table.Rows.Count; i++)
                    if (!table.Rows[i].Cells[4].Text.Contains(tbFilterGP.Text)) table.Rows[i].Visible = false;
            }
        }
        protected void tbFilterPR_TextChanged(object sender, EventArgs e)
        {
            for (int i = 2; i < table.Rows.Count; i++)
                table.Rows[i].Visible = true;

            if (tbFilterPR.Text == "") tbFilterPR.Text = "filter...";
            else
            {
                for (int i = 2; i < table.Rows.Count; i++)
                    if (!table.Rows[i].Cells[5].Text.Contains(tbFilterPR.Text)) table.Rows[i].Visible = false;
            }
        }
        protected void tbFilterRS_TextChanged(object sender, EventArgs e)
        {
            for (int i = 2; i < table.Rows.Count; i++)
                table.Rows[i].Visible = true;
            try
            {
                if (tbFilterRS.Text == "") tbFilterRS.Text = "filter...";
                else
                {
                    string filtertext = tbFilterRS.Text.Trim('+');
                    filtertext = filtertext.Trim('-');

                    if (tbFilterRS.Text.ToCharArray()[0] == '+')
                    {
                        for (int i = 2; i < table.Rows.Count; i++)
                            if (Convert.ToInt32(table.Rows[i].Cells[7].Text) < Convert.ToInt32(filtertext))
                                table.Rows[i].Visible = false;
                    }
                    else if (tbFilterRS.Text.ToCharArray()[0] == '-')
                    {
                        for (int i = 2; i < table.Rows.Count; i++)
                            if (Convert.ToInt32(table.Rows[i].Cells[7].Text) > Convert.ToInt32(filtertext))
                                table.Rows[i].Visible = false;
                    }
                    else
                    {
                        for (int i = 2; i < table.Rows.Count; i++)
                            if (!table.Rows[i].Cells[5].Text.Contains(tbFilterRS.Text))
                                table.Rows[i].Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}